package de.servicezombie.lang;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MapUtils {

	private static final Logger log = LoggerFactory.getLogger(MapUtils.class);
	
	public static <K, V> Map<K, V> emptyMapIfNull(Map<K, V> map) {
		if(map == null) {
			return new HashMap<K, V>();
		}
		return map;
	}

	public static Map<String, String> asMap(Properties props) {
		if (log.isDebugEnabled()) {
			log.debug("asMap: >> " + props);
		}
		
		Map<String, String> map = new HashMap<String, String>();
		if(props == null) {
			return map;
		}
		
		Enumeration<Object> elements = props.keys();
		while(elements.hasMoreElements()) {
			String key = elements.nextElement().toString();
			String value = props.getProperty(key);
			map.put(key, value);
		}
		
		if (log.isDebugEnabled()) {
			log.debug("asMap: << " + map);
		}
		return map;
	}
	
	

}
